﻿using AutoMapper;
using System;
using System.Collections.Generic;
using System.Text;
using xiaodingmarket_application.Interfaces;
using xiaodingmarket_application.ViewModel;
using xiaodingmarket_Domain.Commands.User;
using xiaodingmarket_Domain.Interfaces;
using xiaodingmarket_Domain.Interfaces.IRepository;
using xiaodingmarket_Domain.Models;
using xiaodingmarket_Domain_core.Bus;
using xiaodingmarket_Infra_Data.Repository.EventStore;

namespace xiaodingmarket_application.Service
{
    public class UserService : IUserService
    {
        private readonly IMapper _mapper;
        private readonly IUserRepository _userRepository;
        private readonly IEventStoreRepository _eventStoreRepository;
        private readonly IMediatorHandler Bus;

        public UserService(IMapper mapper,
                           IUserRepository userRepository,
                           IMediatorHandler bus,
                           IEventStoreRepository eventStoreRepository)
        {
            _mapper = mapper;
            _userRepository = userRepository;
            Bus = bus;
            _eventStoreRepository = eventStoreRepository;
        }

        /// <summary>
        /// 数据库注册用户信息
        /// </summary>
        /// <param name="userViewModel"></param>
        public void Create(UserViewModel userViewModel)
        {
            var createCommand = _mapper.Map<CreateUserCommand>(userViewModel);
            Bus.SendCommand(createCommand);
        }

        /// <summary>
        /// 判断有没有注册过(根据易班id来查询数据库)
        /// </summary>
        /// <param name="studentid"></param>
        /// <returns></returns>
        public bool hasCreate(string studentid)
        {
            return _userRepository.hasCreate(studentid);
        }
        /// <summary>
        /// 获取用户的角色
        /// </summary>
        /// <param name="studentid"></param>
        /// <returns></returns>
        //public Role GetuserRole(string studentid)
        //{
        //    Getuserrole
        //}


        public void Dispose()
        {
            GC.SuppressFinalize(this);
        }
        /// <summary>
        /// 根据自定义唯一标识符
        /// </summary>
        /// <param name="studentid"></param>
        /// <returns></returns>
        public UserViewModel GetById(Guid studentid)
        {
            return _mapper.Map<UserViewModel>(_userRepository.GetById(studentid));
        }
        /// <summary>
        /// 根据学号
        /// </summary>
        /// <param name="studentid"></param>
        /// <returns></returns>
        public User GetallBystudentId(string studentid)
        {
            // return _mapper.Map<UserViewModel>(_userRepository.GetUserinfo(studentid));
            return _userRepository.GetUserinfo(studentid);
        }
        public UserViewModel GetBystudentId(string studentid)
        {
            return _mapper.Map<UserViewModel>(_userRepository.GetUserinfo(studentid));
            
        }
        UserViewModel IUserService.GetBystudentId(string userid)
        {
            throw new NotImplementedException();
        }
    }
}
